from enum import Enum, IntEnum


class RiceRoundErrorDef(IntEnum):
    SUCCESS = 0
    HTTP_OK = 200
    HTTP_UNAUTHORIZED = 401
    HTTP_INTERNAL_ERROR = 500
    HTTP_SERVICE_UNAVAILABLE = 503
    HTTP_TIMEOUT = 408
    NETWORK_ERROR = 1
    REQUEST_ERROR = 2
    NO_TOKEN_ERROR = 3
    UNKNOWN_ERROR = 600
    OVER_MAX_ERROR = 998
    ERROR_EXECUTABLE_NOT_FOUND = 1001
    ERROR_START_EXCEPTION = 1002
    ERROR_PROCESS_EXIT = 1003
    ERROR_RESTART_EXCEPTION = 1004
    ERROR_MACHINE_CODE_BASE = 1000
    ERROR_SECRET_TOKEN_BASE = 1100
    ERROR_SECRET_TOKEN = 1101
    ERROR_INSTALL_CLIENT_TOML = 1102

    @staticmethod
    def calc_error_code(base_value, error_code):
        max_value = 1000
        if error_code < max_value:
            if error_code != 0:
                return base_value + error_code
            else:
                return base_value + RiceRoundErrorDef.UNKNOWN_ERROR
        else:
            return base_value + RiceRoundErrorDef.OVER_MAX_ERROR


class RiceTaskErrorDef(IntEnum):
    ERROR_FAILED_TO_FETCH_TASK_TEMPLATE = 2001
    ERROR_INVALID_TASK_TEMPLATE = 2002
    ERROR_FAILED_TO_GET_QUEUE_COUNT = 2003
    ERROR_QUEUE_FULL = 2004
    ERROR_INSUFFICIENT_PERMISSION = 2005
    ERROR_NO_AVAILABLE_MACHINE = 2006
    ERROR_FAILED_TO_DEDUCT_BALANCE = 2007
    ERROR_FAILED_TO_ADD_TASK_TO_QUEUE = 2008
    ERROR_FAILED_TO_SAVE_TASK_RECORD = 2009
    ERROR_INSUFFICIENT_PERMISSION_PAYER_ID_INVALID = 2051
    ERROR_INSUFFICIENT_PERMISSION_PROVIDER_ID_INVALID = 2052
    ERROR_INSUFFICIENT_PERMISSION_BALANCE_FETCH_FAILED = 2053
    ERROR_INSUFFICIENT_PERMISSION_INSUFFICIENT_BALANCE = 2054
